<?php

$CLFetchers->fetchLstDataHeader($Db,
                                $jobId,
                                $SourceDb,
                                $nHeader,
                                $lstHeader);

$_POST["trDateEnt"]       = date("d-m-Y",
                                 strtotime($lstHeader["TR_DATE"][0]));
$_POST["cbCode"]         = $lstHeader["C_B_CODE"][0];
$_POST["cbName"]         = $lstHeader["C_B_NAME"][0];
$_POST["typeEnt"]        = $lstHeader["IN_OUT"][0];
$_POST["currencyEnt"]    = $lstHeader["CURRENCY"][0];
$_POST["rateEnt"]        = $lstHeader["RATE"][0];
$_POST["suppCstCodeEnt"] = $lstHeader["SUPPLIER_CUSTOMER"][0];
$_POST["suppCstNameEnt"] = $lstHeader["SUPPLIER_CUSTOMER_NAME"][0];
$_POST["txEnt"]          = $lstHeader["CASH_BANK_TX_NAME"][0];
$_POST["memoEnt"]        = $lstHeader["MEMO"][0];

$CLFetchers->fetchLstDataDetail($Db,
                                $jobId,
                                $SourceDb->fetchValue("srcDbDtl"),
                                $nDetail,
                                $lstDetail);

$_POST["totAmount"] = $lstDetail["TOT_AMOUNT"][0];
$_POST["totNet"]    = $lstDetail["TOT_NET"][0];

$CLFetchers->fetchLstDataSubDetail($Db,
                                   $jobId,
                                   $SourceDb,
                                   $nSubDetail,
                                   $lstSubDetail);

for($iRow = 0; $iRow < $nSubDetail; $iRow++){

  $_POST["glAccEnt"][$iRow]         = $lstSubDetail["GL_ACC_NAME"][$iRow];
  $_POST["glAccCodeEnt"][$iRow]     = $lstSubDetail["GL_ACC"][$iRow];
  $_POST["glSubAccEnt"][$iRow]      = $lstSubDetail["GL_SUB_ACC_NAME"][$iRow];
  $_POST["glSubAccCodeEnt"][$iRow]  = $lstSubDetail["GL_SUB_ACC"][$iRow];
  $_POST["descpEnt"][$iRow]         = $lstSubDetail["DESCRIPTION"][$iRow];
  $_POST["amountEnt"][$iRow]        = $lstSubDetail["AMOUNT"][$iRow];
  $_POST["netEnt"][$iRow]           = $lstSubDetail["NET"][$iRow];
}

echo  $GJs->includeThis(_TYPE_CSS,"/ecs/myMenu.css").
      $GJs->includeThis(_TYPE_JS ,"/ecs/javascript/GMisc.js").
      $GJs->includeThis(_TYPE_JS ,"/ecs/javascript/GAjax.js").
      $GJs->includeThis(_TYPE_JS ,"../javascript/misc.js").
      $GJs->includeThis(_TYPE_JS ,"../javascript/ajax.js").
      $GJs->stopRKey();

if($_POST["txEnt"] != ""){

  echo  $GJs->jsScript(_TYPE_JS, "Ajax.setButton(\"txEnt\",
                                                 \"txButton\");");
}else{

  /*Do nothing*/
}

echo  /**
      * Global header
      *
      */
      "<table border='1' class='dedeq' >\n".
      "<tr>\n".

      "<td class='solid'> DATE </td>

          <td > ".$_POST["trDateEnt"]." </td> \n".

      "</tr>\n".
      "<tr>\n".

      "<td class='solid'> Cash-Bank </td>
        <td> <table>
             <tr>

             <td> ".$_POST["cbCode"]." </td>

             <td> ".$_POST["cbName"]." </td>
             </tr>
             </table> </td>\n".

      "</tr>\n".
      "<tr>\n".

      "<td class='solid'> Type     </td>
        <td> ".$GHtml->formInput("typeEnt",
                                 $_POST["typeEnt"],
                                 _MODE_READONLY)." </td>\n".

      "</tr>\n".
      "<tr>\n".

      "<td class='solid'> Currency </td>
        <td> ".$_POST["currencyEnt"]." </td>\n".

      "</tr>\n".
      "<tr>\n".

      "<td class='solid'> Rate     </td>
        <td> ".number_format($_POST["rateEnt"],2,",",".")." </td>\n".

      "</tr>\n".
      "<tr>\n".

      "<td name='supplierCustomer'  class='solid'>  </td>
        <td> <table border='0'>
             <tr>

             <td> ".$_POST["suppCstNameEnt"]." </td>

             <td> ".$_POST["suppCstCodeEnt"]." </td>
             </tr>
             </table> </td>\n".

      "</tr>\n".
      "<tr>\n".

      "<td class='solid'> Transaction </td>
        <td> ".$_POST["txEnt"]." </td>\n".

      "</tr>\n".
      "<tr>\n".

      "<td class='solid'> Memo        </td>
        <td> ".$_POST["memoEnt"]."   </td>\n".

      "</tr>\n".
      "</table><br/>\n";

echo  "<table border='1' class='dedeq'>\n".
      "<tr>\n".

      "<th> No                        </th>\n".

      "<th> Account <br/> Sub Account </th>\n".

      "<th> Description               </th>\n".

      "<th> Amount                    </th>\n".

      "<th> Net                       </th>\n".

      "</tr>\n";

for($iRow = 0; $iRow < $nSubDetail; $iRow++){

  echo  "<tr>\n".

        "<td> ".($iRow + 1)." </td>\n".

        "<td> <table border=0>
              <tr>

              <td> ".$_POST["glAccEnt"][$iRow]." </td>

              <td> ".$_POST["glAccCodeEnt"][$iRow]." </td>

              <td> ".$_POST["glSubAccEnt"][$iRow]." </td>

              <td> ".$_POST["glSubAccCodeEnt"][$iRow]." </td>

              </tr>
              </table></td>\n".

      "<td> ".$_POST["descpEnt"][$iRow]." </td>\n".

      "<td class='isNumber'> ".number_format($_POST["amountEnt"][$iRow],2,",",".")." </td>\n".

      "<td class='isNumber'> ".number_format($_POST["netEnt"][$iRow],2,",",".")." </td>\n".

      "</tr>\n";
}

echo  "<tr>\n".

      "<td ".$GHtml->rowColSpan( 1, 2)." align='center'><b> TOTAL </b></td>\n".

      "<td name='txButton' align='center'> </td>\n".

      "<td class='isNumber'> ".number_format($_POST["totAmount"],2,",",".")." </td>\n".

      "<td class='isNumber'> ".number_format($_POST["totNet"],2,",",".")." </td>\n".

      "</tr>\n".
      "</table><br/>\n";

echo  $GJs->jsScript("","Misc.setSuppCst(\"typeEnt\",
                                         \"supplierCustomer\");
                         Ajax.setButton(\"txEnt\",
                                        \"txButton\");");

switch($_POST["txEnt"]){

  case _CASH_BANK_INVOICE_NAME:

    $caption = "Invoice";

    $CLFetchers->fetchDtlInvoice($Db,
                                 $jobId,
                                 $SourceDb->fetchValue("srcDbDtlIvcSupplier"),
                                 $nDetail,
                                 $lstDetail);

    if($nDetail > 0){

      $_POST["totInvoice"]  = $lstDetail["TOT_INVOICE"][0];
      $_POST["totPayment"]  = $lstDetail["PAYMENT"][0];
      $_POST["gainEnt"]     = $lstDetail["GAIN_LOSS"][0];
      $_POST["discEnt"]     = $lstDetail["DISCOUNT"][0];

      $CLFetchers->fetchSubInvoice($Db,
                                   $jobId,
                                   $SourceDb->fetchValue("srcDbSubIvcSupplier"),
                                   $nSubDetail,
                                   $lstSubDetail);

      /**
       * this made up to solve
       * the previous currencyEnt which was not
       * an array
       */
      $_POST["currencyEnt"] = array();

      for($iRow = 0; $iRow < $nSubDetail ; $iRow++){

        $_POST["notaSrcEnt"][$iRow]   = $lstSubDetail["REFERENCE"][$iRow];
        $_POST["trDateEnt"][$iRow]    = date("d-m-Y",
                                             strtotime($lstSubDetail["TR_DATE"][$iRow]));
        $_POST["dueDateEnt"][$iRow]   = date("d-m-Y",
                                             strtotime($lstSubDetail["DUE_DATE"][$iRow]));
        $_POST["currencyEnt"][$iRow]  = $lstSubDetail["CURRENCY"][$iRow];
        $_POST["rateEnt"][$iRow]      = $lstSubDetail["RATE"][$iRow];
        $_POST["remainEnt"][$iRow]    = $lstSubDetail["REMAIN"][$iRow];
        $_POST["paymentEnt"][$iRow]   = $lstSubDetail["AMOUNT"][$iRow];
      }
    }
    break;

  case _CASH_BANK_CCL_DP_NAME:

    $caption = "DP Cancel";

    $CLFetchers->fetchDtlDp($Db,
                            $jobId,
                            $SourceDb->fetchValue("srcDbDtlCclDpSupplier"),
                            $nDetail,
                            $lstDetail);

    if($nDetail > 0){

      $_POST["totInvoice"]  = $lstDetail["TOT_INVOICE"][0];
      $_POST["totPayment"]  = $lstDetail["PAYMENT"][0];
      $_POST["gainEnt"]     = $lstDetail["GAIN_LOSS"][0];
      $_POST["discEnt"]     = $lstDetail["DISCOUNT"][0];

      $CLFetchers->fetchSubDp($Db,
                              $jobId,
                              $SourceDb->fetchValue("srcDbSubCclDpSupplier"),
                              $nSubDetail,
                              $lstSubDetail);

      /**
       * this made up to solve
       * the previous currencyEnt which was not
       * an array
       */
      $_POST["currencyEnt"] = array();

      for($iRow = 0; $iRow < $nSubDetail ; $iRow++){

        $_POST["notaSrcEnt"][$iRow]   = $lstSubDetail["REFERENCE"][$iRow];
        $_POST["trDateEnt"][$iRow]    = date("d-m-Y",
                                             strtotime($lstSubDetail["TR_DATE"][$iRow]));
        $_POST["dueDateEnt"][$iRow]   = date("d-m-Y",
                                             strtotime($lstSubDetail["DUE_DATE"][$iRow]));
        $_POST["currencyEnt"][$iRow]  = $lstSubDetail["CURRENCY"][$iRow];
        $_POST["rateEnt"][$iRow]      = $lstSubDetail["RATE"][$iRow];
        $_POST["remainEnt"][$iRow]    = $lstSubDetail["REMAIN"][$iRow];
        $_POST["paymentEnt"][$iRow]   = $lstSubDetail["AMOUNT"][$iRow];
      }
    }

    break;

  default:
}

echo  "<table border='1' class='dedeq'>
       <caption> ".$caption." </caption>\n";

switch($_POST["txEnt"]){

  case _CASH_BANK_INVOICE_NAME:

    echo  "<tr>\n".
          "<th> No        </th>\n".
          "<th> Invoice   </th>\n".
          "<th> Date      </th>\n".
          "<th> Due       </th>\n".
          "<th> Whose     </th>\n".
          "<th> Currency  </th>\n".
          "<th> Rate      </th>\n".
          "<th> Remain    </th>\n".
          "<th> Payment   </th>\n".
          "</tr>\n";

    for($iRow = 0; $iRow < $nSubDetail; $iRow++){

      echo  "<tr>\n".

            "<td> ".($iRow + 1)." </td>\n".

            "<td> ".$_POST["notaSrcEnt"][$iRow]." </td>\n".

            "<td> ".$_POST["trDateEnt"][$iRow]." </td>\n".

            "<td> ".$_POST["dueDateEnt"][$iRow]." </td>\n".

            "<td> ".$_POST["suppCstNameEnt"]." </td>\n".

            "<td> ".$_POST["currencyEnt"][$iRow]." </td>\n".

            "<td> ".$_POST["rateEnt"][$iRow]." </td>\n".

            "<td class='isNumber'> ".number_format($_POST["remainEnt"][$iRow],2,",",".")." </td>\n".

            "<td class='isNumber'> ".number_format($_POST["paymentEnt"][$iRow],2,",",".")." </td>\n".

            "</tr>\n";
    }

    echo  "<tr>\n".

          "<td ".$GHtml->rowColSpan( 1, 7)." align='right'>  </td>\n".

          "<td> #Invoice </td>\n".

          "<td class='isNumber'> ".number_format($_POST["totInvoice"],2,",",".")." </td>\n".

          "</tr>\n".
          "<tr>\n".

          "<td ".$GHtml->rowColSpan( 1, 7)." align='right'>  </td>\n".

          "<td> #Payment </td>\n".

          "<td class='isNumber'> ".number_format($_POST["totPayment"],2,",",".")." </td>\n".

          "</tr>\n".
          "<tr>\n".

          "<td ".$GHtml->rowColSpan( 1, 7)." align='right'>  </td>\n".

          "<td> #Gain On Rate </td>\n".

          "<td class='isNumber'> ".number_format($_POST["gainEnt"],2,",",".")." </td>\n".

          "</tr>\n".
                                    "<tr>\n".

          "<td ".$GHtml->rowColSpan( 1, 7)." align='right'>  </td>\n".

          "<td> #Discount </td>\n".

          "<td class='isNumber'> ".number_format($_POST["discEnt"],2,",",".")." </td>\n".

          "</tr>\n".

          "</table><br/>\n";
    break;

  case _CASH_BANK_CCL_DP_NAME:

    echo  "<tr>\n".
          "<th> No        </th>\n".
          "<th> Invoice   </th>\n".
          "<th> Date      </th>\n".
          "<th> Due       </th>\n".
          "<th> Whose     </th>\n".
          "<th> Currency  </th>\n".
          "<th> Rate      </th>\n".
          "<th> Remain    </th>\n".
          "<th> Payment   </th>\n".
          "</tr>\n";

    for($iRow = 0; $iRow < $nSubDetail; $iRow++){

      echo  "<tr>\n".

            "<td> ".($iRow + 1)." </td>\n".

            "<td> ".$_POST["notaSrcEnt"][$iRow]." </td>\n".

            "<td> ".$_POST["trDateEnt"][$iRow]." </td>\n".

            "<td> ".$_POST["dueDateEnt"][$iRow]." </td>\n".

            "<td> ".$_POST["suppCstNameEnt"]." </td>\n".

            "<td> ".$_POST["currencyEnt"][$iRow]." </td>\n".

            "<td> ".$_POST["rateEnt"][$iRow]." </td>\n".

            "<td class='isNumber'> ".number_format($_POST["remainEnt"][$iRow],2,",",".")." </td>\n".

            "<td class='isNumber'> ".number_format($_POST["paymentEnt"][$iRow],2,",",".")." </td>\n".

            "</tr>\n";
    }

    echo  "<tr>\n".

          "<td ".$GHtml->rowColSpan( 1, 7)." align='right'>  </td>\n".

          "<td> #Invoice </td>\n".

          "<td class='isNumber'> ".number_format($_POST["totInvoice"],2,",",".")." </td>\n".

          "</tr>\n".
          "<tr>\n".

          "<td ".$GHtml->rowColSpan( 1, 7)." align='right'>  </td>\n".

          "<td> #Payment </td>\n".

          "<td class='isNumber'> ".number_format($_POST["totPayment"],2,",",".")." </td>\n".

          "</tr>\n".
          "<tr>\n".

          "<td ".$GHtml->rowColSpan( 1, 7)." align='right'>  </td>\n".

          "<td> #Gain On Rate </td>\n".

          "<td class='isNumber'> ".number_format($_POST["gainEnt"],2,",",".")." </td>\n".

          "</tr>\n".
                                    "<tr>\n".

          "<td ".$GHtml->rowColSpan( 1, 7)." align='right'>  </td>\n".

          "<td> #Discount </td>\n".

          "<td class='isNumber'> ".number_format($_POST["discEnt"],2,",",".")." </td>\n".

          "</tr>\n".

          "</table><br/>\n";
    break;

  default:
}

?>